#include <stdio.h>

//字符串复制
char *my_Strcpy(char* dest,const char* src)
{
    char *r = dest;
    while(*dest++ = *src++);
    return r;
}

//字符串求长度
size_t myStrlen(const char* str) {
	assert(str != NULL);
	size_t size = 0;
	while (str[size] != '\0') {
		size++;
	}
	return size;
}

//字符串拼接
char* myStrcat(char* dest, const char* src) {
	assert(dest != NULL);
	assert(src != NULL);
	int destTail = 0;
	while (dest[destTail] != '\0') {
		destTail++;
	}
	//myStrcpy(dest + destTail, src);
	int i = 0;
	while (src[i] != '\0') {
		dest[destTail + i] = src[i];
		i++;
	}
	dest[destTail + i] = '\0';
	return dest;
} 


//字符串大小比较、是否相等
int myStrcmp(const char* str1, const char* str2) {
	assert(str1 != NULL);
	assert(str2 != NULL);
	const char* p1 = str1;
	const char* p2 = str2;
	while (*p1 != '\0' && *p2 != '\0') {
		if (*p1 < *p2) {
			return -1;
		}
		else if (*p1 > *p2) {
			return 1;
		}
		else {
			p1++;
			p2++;
		}
	}
	/*if (*p1 < *p2) {
		return -1;
	}
	else if(*p1>*p2){
		return 1;
	}
	else {
		return 0;
	}*/
	return *p1 - *p2;
}
//以src地址开始的前num个字节拷贝到dest中
char* myStrncpy(char* dest, char* src, size_t num) {
	assert(dest != NULL);
	assert(src != NULL);
	assert(num != 0);
	size_t i = 0;
	while (src[i] != '\0'&&i<num) {
		dest[i] = src[i];
		i++;
	}
	while (i < num) {
		dest[i] = '\0';
		i++;
	}
	return dest;
}

//将src的前num个字节追加到dest后,会补\0
char* myStrncat(char* dest, const char* src, size_t num) {
	assert(dest != NULL);
	assert(src != NULL);
	assert(num != 0);
	size_t destTail = 0;
	while (dest[destTail] != '\0') {
		destTail++;
	}
	size_t i = 0;
	while (src[i] != '\0' && i < num) {
		dest[destTail + i] = src[i];
		i++;
	}
	dest[destTail + i] = '\0';
	return dest;
}

//比较str1和str2的前num个字节
int myStrncmp(const char* str1, const char* str2, size_t num) {
	assert(str1 != NULL);
	assert(str2 != NULL);
	assert(num != 0);
	size_t i = 0;
	while (str1[i] != '\0' && str2[i] != '\0' && i < num) {
		if (str1[i] < str2[i]) {
			return -1;
		}
		else if (str1[i] > str2[i]) {
			return 1;
		}
		else {
			i++;
		}
	}
	if (i == num) {
		return 0;
	}
	return str1[i] - str2[i];
}

//判定一个字符串是否包含另一个
const char* myStrstr(const char* str1, const char* str2) {
	assert(str1 != NULL);
	assert(str2 != NULL);
	assert(*str1 != '\0');
	assert(*str2 != '\0');

	const char* black = str1;
	while (*black != '\0') {
		const char* red = black;
		const char* sub = str2;
		while (*red!='\0'&&*sub!='\0'
			&&*red == *sub) {
			red++;
			sub++;
		}
		//以上循环结束,有3种可能
		//1.*red==\0 直接结束循环 进入下次
		//2.*sub==\0 找到了 返回black
		//3.*red!=*sub 直接进入下次循环
		/*if (*red == '\0' || *red == *sub) {
			black++;
			continue;
		}*/
		if (*sub == '\0') {
			return black; 
		}
		black++;
	}
	return NULL;
}




int main()
{
    int A[5]={1,5,2,8,4};

    return 0;
}